package com.chao.common.codeGeneration.source;

import com.chao.common.codeGeneration.bean.Table;
import com.chao.common.codeGeneration.bean.TableColumns;
import com.chao.common.codeGeneration.generation.FileUtile;
import com.chao.common.codeGeneration.generation.Generation;
import com.chao.common.util.DateUtile;
import com.chao.common.util.exception.SysException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

public class ToFeign {

    public static void toFeign(Table table, String path){
        File file = FileUtile.createFile(path+"\\feign\\"+table.getPojoKey()+"Mapper.java");
        try(Writer out =new FileWriter(file)) {
            String servicePag = table.getService();
            List<String> fun = Arrays.asList(table.getFunction());
            StringBuilder sb = new StringBuilder();
            // package
            sb.append("package com.chao.").append(servicePag).append(".modular.").append(table.getPacName()).append(".feign;").append("\r\n\r\n");

            //import
            sb.append("import com.alibaba.fastjson.JSONObject;").append("\r\n");
            sb.append("import com.chao.").append(servicePag).append(".modular.").append(table.getPacName()).append(".bean.").append(table.getPojoKey()).append(";").append("\r\n");
            sb.append("import org.springframework.cloud.openfeign.FeignClient;").append("\r\n");
            sb.append("import org.springframework.web.bind.annotation.PostMapping;").append("\r\n");
            sb.append("import org.springframework.web.bind.annotation.RequestBody;").append("\r\n");
            sb.append("import org.springframework.web.bind.annotation.RequestParam;").append("\r\n");
            if(fun.contains("批量新增")){
                sb.append("import java.util.List;").append("\r\n");
            }
            if(fun.contains("分页查询") || fun.contains("查询列表")){
                sb.append("import com.chao.common.util.reaction.PageList;").append("\r\n");
            }


            //Class
            sb.append("\r\n").append("\r\n");
            sb.append("/**").append("\r\n");
            sb.append(" * ").append(table.getPojoName()).append("Feign 消费端").append("\r\n");
            sb.append(" * ").append(Generation.author).append("\r\n");
            sb.append(" * ").append(DateUtile.toYmd()).append("\r\n");
            sb.append(" */").append("\r\n");
            sb.append("@FeignClient(name = \"${sys.db}/").append(table.getUpojoKey()).append("\")").append("\r\n");
            sb.append("public interface ").append(table.getPojoKey()).append("Mapper {").append("\r\n");

            //函数
            //获取主键
            TableColumns col = null;
            for(TableColumns column: table.getColumns()){
                if(column.getKey()){
                    col = column;
                }
            }
            if(col == null){
                throw new SysException("数据表未设置主键!");
            }
            if(fun.contains("新增")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 新增").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/insert").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tInteger insert").append(table.getPojoKey()).append("(@RequestBody ").append(table.getPojoKey()).append(" ").append(table.getUpojoKey()).append(");").append("\r\n");
            }

            if(fun.contains("批量新增")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 批量新增").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/insertAll").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tInteger insertAll").append(table.getPojoKey()).append("(@RequestBody List<").append(table.getPojoKey()).append("> ").append(table.getUpojoKey()).append("List);").append("\r\n");
            }

            if(fun.contains("修改")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 修改").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/update").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tInteger update").append(table.getPojoKey()).append("(@RequestBody ").append(table.getPojoKey()).append(" ").append(table.getUpojoKey()).append(");").append("\r\n");
            }

            if(fun.contains("逻辑删除")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 删除").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/delete").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tInteger delete").append(table.getPojoKey()).append("(@RequestParam(\"").append(col.getLowercaseName()).append("\") String ").append(col.getLowercaseName()).append(");").append("\r\n");
            }

            if(fun.contains("物理删除")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 物理删除").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/deleteData").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tInteger deleteData").append(table.getPojoKey()).append("(@RequestParam(\"").append(col.getLowercaseName()).append("\") String ").append(col.getLowercaseName()).append(");").append("\r\n");
            }

            if(fun.contains("查询详情")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 查询").append(table.getPojoName()).append("详情").append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/select").append(table.getPojoKey()).append("ById\")").append("\r\n");
                sb.append("\t").append(table.getPojoKey()).append(" select").append(table.getPojoKey()).append("By").append(col.getCapitalLetterName()).append("(@RequestParam(\"").append(col.getLowercaseName()).append("\") String ").append(col.getLowercaseName()).append(");").append("\r\n");
            }

            if(fun.contains("查询列表") || fun.contains("分页查询")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 查询").append(table.getPojoName()).append("列表").append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@PostMapping(\"/select").append(table.getPojoKey()).append("ByList\")").append("\r\n");
                sb.append("\tPageList<").append(table.getPojoKey()).append("> select").append(table.getPojoKey()).append("ByList(@RequestBody JSONObject json);").append("\r\n");
            }

            sb.append("\r\n");
            sb.append("}");
            out.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}